//
//  HHLanguageProjectController.m
//  HHgitosc
//
//  Created by hjl on 16/3/22.
//  Copyright © 2016年 hjl. All rights reserved.
//

#import "HHLanguageProjectController.h"
#import <MJRefresh.h>
#import <AFNetworking.h>
#import "HHProject.h"
#import <MJExtension.h>
#import "HHProjectCell.h"
#import "HHInfoViewController.h"
@interface HHLanguageProjectController ()

/** 项目模型数组 */
@property (strong, nonatomic) NSMutableArray *projects;

/** 页码 */
@property (nonatomic, assign) NSInteger page;

@end

static NSString * const HHProjectCellId = @"project";

@implementation HHLanguageProjectController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTableView];
    [self setupRefresh];
    
}

- (void)setupRefresh{
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewProjects)];
    
    [self.tableView.mj_header beginRefreshing];
    
    // 自动改变透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loafMoreProjects)];
}

- (void)loadNewProjects{
    //@"https://git.oschina.net/api/v3/projects/languages/5?"
    NSString *strUrl = @"https://git.oschina.net/api/v3/projects/languages";
    // 语言的链接地址
    strUrl = [NSString stringWithFormat:@"%@/%ld?",strUrl, (long)self.languageID ];
    
    // 结束上拉
    [self.tableView.mj_footer endRefreshing];
    
    [[AFHTTPSessionManager manager] GET:strUrl parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.projects = [HHProject mj_objectArrayWithKeyValuesArray:responseObject];
        
        [self.tableView reloadData];
        
        [self.tableView.mj_header endRefreshing];
        
        self.page = 1;
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)loafMoreProjects{
    
    NSString *strUrl = @"https://git.oschina.net/api/v3/projects/languages";
    // 语言的链接地址
    strUrl = [NSString stringWithFormat:@"%@/%ld?",strUrl, (long)self.languageID ];
    // 结束下拉
    [self.tableView.mj_header endRefreshing];
    
    _page++;
  
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"page"] = @(_page);
    
    [[AFHTTPSessionManager manager] GET:strUrl parameters:parame progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *projects = [HHProject mj_objectArrayWithKeyValuesArray:responseObject];
        
        [self.projects addObjectsFromArray:projects];
        
        [self.tableView reloadData];
        
        [self.tableView.mj_footer endRefreshing];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        _page--;
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)setupTableView{
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HHProjectCell class]) bundle:nil] forCellReuseIdentifier:HHProjectCellId];
    
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 44, 0);
    
    self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    self.tableView.mj_footer.hidden = (self.projects.count == 0);
    
    return self.projects.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HHProjectCell *cell = [tableView dequeueReusableCellWithIdentifier:HHProjectCellId forIndexPath:indexPath];
    
    HHProject *project = self.projects[indexPath.row];
    
    cell.project = project;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    HHProject *project = self.projects[indexPath.row];
    return project.cellHeight;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HHProject * project = self.projects[indexPath.row];
    
    HHInfoViewController *infoVc = [[HHInfoViewController alloc] init];
    
    infoVc.project = project;
    
    [self.navigationController pushViewController:infoVc animated:YES];
}





@end
